const mongoose = require('mongoose');
const { Schema } = mongoose;
const isEmpty = require('lodash.isempty');
const MonthRecordSchema = new mongoose.Schema({
  "Jan": {
    type: [Number]
  },
  "Feb": {
    type: [Number]
  },
  "Mar": {
    type: [Number]
  },
  "Apr": {
    type: [Number]
  },
  "May": {
    type: [Number]
  },
  "Jun": {
    type: [Number]
  },
  "Jul": {
    type: [Number]
  },
  "Aug": {
    type: [Number]
  },
  "Sep": {
    type: [Number]
  },
  "Oct": {
    type: [Number]
  },
  "Nov": {
    type: [Number]
  },
  "Dec": {
    type: [Number]
  },
})

const recordSchema = new mongoose.Schema({
  year: {
    type: Number,
    require: [true, "year is required"],
    unique: true
  },
  record: Schema.Types.Mixed,
  updatedAt: {
    type: Date,
    default: () => Date.now()
  },
}, { collection: 'dateRecord' })

recordSchema.statics.findRecord = async function (year, month) {
  queryMonth = "record." + month;

  const result = await this.findOne({ year, queryMonth: month }, { [`record.${month}`]: 1 });
  if (result) {
    const record = result.record;
    if (!isEmpty(record)) {
      return record[month];
    } 
  }else {
    // throw new Error("no record found")
    return [];
  }

}

module.exports = mongoose.model('dateRecord', recordSchema);